package com.example.klinedemo.play

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.Manifest
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Environment
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.klinedemo.R
import java.io.File

class PlayActivity : AppCompatActivity() {
    // 添加请求码常量
    private companion object {
        const val REQUEST_CODE = 1001
    }


    var h264Player:H264Player? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_play)

        if (!checkPermissions()) {
            requestPermissions()
        }

        initSurfaceView()
    }

    /**
     * 初始化 initSurfaceView
     */
    private fun initSurfaceView() {
        // surfaceView 只是显示控件 画框
        // holder.surface 相当于画布
       val surfaceView = findViewById<SurfaceView>(R.id.preView)
        surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                // holder.surface 相当于画布
                h264Player = H264Player(
                    File(Environment.getExternalStorageDirectory(), "test.h264"),
                    holder.surface
                )

                h264Player?.play()
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {

            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {

            }

        })
    }

    // 检查权限状态
    private fun checkPermissions(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) == PERMISSION_GRANTED
        } else {
            ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED
        }
    }

    // 请求权限
    private fun requestPermissions() {
        val permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arrayOf(Manifest.permission.READ_MEDIA_IMAGES)
        } else {
            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
        }

        ActivityCompat.requestPermissions(
            this,
            permissions,
            REQUEST_CODE
        )
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            REQUEST_CODE -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PERMISSION_GRANTED) {
                    // 权限已授予，继续操作
                } else {
                    // 权限被拒绝，提示用户或关闭页面
                    Toast.makeText(this, "需要权限才能使用该功能", Toast.LENGTH_SHORT).show()
                    finish()
                }
            }
        }
    }


}